using System;
using System.Resources;
using System.Reflection;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using Zip;
using Win32Mapi;

namespace ZipStudio
{
    /// <summary>
    /// Summary description for ZipForm.
    /// </summary>
    public class ZipForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button btnDone;
        private System.Windows.Forms.CheckBox chkOpenFolder;
        private System.Windows.Forms.GroupBox grpActions;
        private System.Windows.Forms.Button btnCreateZipFile;
        private System.Windows.Forms.CheckBox chkOpenFile;
        private System.Windows.Forms.SaveFileDialog savZipFileDlg;
        private IContainer components;

        private ListViewColumnSorter _columnSorter = null;
        private string[] _filePaths = null;
        private string[] _includeFilePaths = null;
        private Hashtable _excludeItems = new Hashtable();
        private ZipFormData _data = null;
        private ImageList imageList1;
        private GroupBox grpFileLocation;
        private Button btnFilepath;
        private Label lblFilepath;
        private TextBox txtFilepath;
        private GroupBox grpFiles;
        private CheckBox checkBox1;
        private CheckBox chkIncludeMisc;
        private CheckBox chkSaveSettings;
        private Button btnResetList;
        private Button btnRemoveSelection;
        private CheckBox chkModifiedFiles;
        private CheckBox chkFullPathInfo;
        private ListView lstFiles;
        private ColumnHeader Filename;
        private ColumnHeader Filepath;
        private ColumnHeader DateModified;
        private CheckBox chkZipAndMail;
        private ErrorProvider errorProvider1;
        private GroupBox grpMailInfo;
        private TextBox txtObject;
        private TextBox txtAddress;

        public ZipFormData Data
        {
            get { return _data; }
            set { _data = value; }
        }

        private bool dataSetting = false;
        private void SetFormData()
        {
            dataSetting = true;
            string openFile = _data.Value(ZipFormData.DataName.SZOpenFile);
            this.chkOpenFile.Checked =
                (openFile == null) || (openFile == "") ||
                (openFile == "false") ? false : true;
            string openFolder = _data.Value(ZipFormData.DataName.SZOpenFolder);
            this.chkOpenFolder.Checked =
                (openFolder == null) || (openFolder == "") ||
                (openFolder == "false") ? false : true;
            this.txtFilepath.Text = _data.Value(ZipFormData.DataName.SZPath);
            string fullPathInfo = _data.Value(ZipFormData.DataName.SZFullPathInfo);
            // Added support to save other save settings
            this.chkFullPathInfo.Checked = string.IsNullOrEmpty(fullPathInfo) || fullPathInfo == "false" ? false : true;
            string includeMisc = _data.Value(ZipFormData.DataName.SZIncludeMisc);
            this.chkIncludeMisc.Checked = string.IsNullOrEmpty(includeMisc) || includeMisc == "false" ? false : true;
            string modifiedFiles = _data.Value(ZipFormData.DataName.SZModifiedFiles);
            this.chkModifiedFiles.Checked = string.IsNullOrEmpty(modifiedFiles) || modifiedFiles == "false" ? false : true;
            dataSetting = false;
        }
        private void GetFormData()
        {
            _data.Value(ZipFormData.DataName.SZOpenFile,
                this.chkOpenFile.Checked ? "true" : "false");
            _data.Value(ZipFormData.DataName.SZOpenFolder,
                this.chkOpenFolder.Checked ? "true" : "false");
            _data.Value(ZipFormData.DataName.SZPath,
                this.txtFilepath.Text);
            // Added support to save other save settings
            _data.Value(ZipFormData.DataName.SZModifiedFiles,
                this.chkModifiedFiles.Checked ? "true" : "false");
            _data.Value(ZipFormData.DataName.SZFullPathInfo,
                this.chkFullPathInfo.Checked ? "true" : "false");
            _data.Value(ZipFormData.DataName.SZIncludeMisc,
                this.chkIncludeMisc.Checked ? "true" : "false");
        }
        private string[] SetFileGrid(ListView List, string[] FilePaths,
            bool OnlyChangedFiles, DateTime ChangeDate, bool IncludeMiscFiles,
            Hashtable ExcludeItems)
        {
            ArrayList includes = new ArrayList();
            // Test for selected items first...
            if (ExcludeItems != null)
            {
                foreach (ListViewItem item in List.SelectedItems)
                {
                    string select = item.SubItems[1].Text + @"\" +
                        item.SubItems[0].Text;
                    ExcludeItems[select] = select;
                }
            }
            List.BeginUpdate();
            List.Items.Clear();
            foreach (string filePath in FilePaths)
            {
                // Set a default icon for the file.

                FileInfo fi = new FileInfo(filePath);
                if (fi.Exists)
                {
                    DateTime fileDate = fi.LastWriteTime;
                    bool include =
                        OnlyChangedFiles &&
                        (ChangeDate.CompareTo(fi.LastWriteTime) > 0) ?
                        false : true;
                    if (((FileItem)_data.Files[filePath]).MiscFileItem)
                    {
                        include = IncludeMiscFiles ? include : false;
                    }
                    if (include && (ExcludeItems[filePath] == null))
                    {
                        if (!imageList1.Images.ContainsKey(fi.Extension))
                        {
                            // If not, add the image to the image list.
                            Icon iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(fi.FullName);
                            imageList1.Images.Add(fi.Extension, iconForFile);
                        }
                        string[] subItems = new string[3];
                        subItems[0] = filePath.Replace(fi.DirectoryName, "");
                        if (subItems[0].StartsWith("\\"))
                            subItems[0] = subItems[0].Substring(1);
                        subItems[1] = fi.DirectoryName;
                        subItems[2] = fileDate.ToString("yyyy/MM/dd hh:mm tt");
                        ListViewItem item = new ListViewItem(subItems, fi.Extension);
                        List.Items.Add(item);
                        includes.Add(filePath);
                    }
                }
            }
            this.lstFiles.Sort();
            includes.Sort();
            List.EndUpdate();
            return includes.ToArray(typeof(string)) as string[];
        }
        private void UpdateFileDisplayStatus()
        {
            this.grpFiles.Text = "Included " +
                this.lstFiles.Items.Count.ToString() + " files:";
            this.btnCreateZipFile.Enabled =
                this.lstFiles.Items.Count > 0;
        }
        public ZipForm(string[] FilePaths, ZipFormData Data)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.Text = ZipFormData.AppString("Title");
            _columnSorter = new ListViewColumnSorter(this.lstFiles);
            this.lstFiles.ListViewItemSorter = _columnSorter;
            _columnSorter.ColumnToSort = 0;
            _columnSorter.SortOrder = SortOrder.Ascending;
            _data = Data;
            if ((_data.Value(ZipFormData.DataName.SZPath) == null) ||
                (_data.Value(ZipFormData.DataName.SZPath) == ""))
            {
                _data.Value(ZipFormData.DataName.SZPath,
                    @"c:\" + ZipFormData.AppString("Name") + ".zip");
            }
            this.SetFormData();
            _filePaths = FilePaths;
            _includeFilePaths = this.SetFileGrid(this.lstFiles, _filePaths,
                this.chkModifiedFiles.Checked, _data.StartupTime,
                this.chkIncludeMisc.Checked, _excludeItems);
            this.chkSaveSettings.Checked = _data.SaveSettings;
            this.UpdateFileDisplayStatus();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.Label label2;
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ZipForm));
            System.Windows.Forms.Label label1;
            this.btnDone = new System.Windows.Forms.Button();
            this.btnCreateZipFile = new System.Windows.Forms.Button();
            this.chkOpenFolder = new System.Windows.Forms.CheckBox();
            this.grpActions = new System.Windows.Forms.GroupBox();
            this.chkOpenFile = new System.Windows.Forms.CheckBox();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.savZipFileDlg = new System.Windows.Forms.SaveFileDialog();
            this.grpFileLocation = new System.Windows.Forms.GroupBox();
            this.btnFilepath = new System.Windows.Forms.Button();
            this.lblFilepath = new System.Windows.Forms.Label();
            this.txtFilepath = new System.Windows.Forms.TextBox();
            this.grpFiles = new System.Windows.Forms.GroupBox();
            this.chkZipAndMail = new System.Windows.Forms.CheckBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.chkIncludeMisc = new System.Windows.Forms.CheckBox();
            this.chkSaveSettings = new System.Windows.Forms.CheckBox();
            this.btnResetList = new System.Windows.Forms.Button();
            this.btnRemoveSelection = new System.Windows.Forms.Button();
            this.chkModifiedFiles = new System.Windows.Forms.CheckBox();
            this.chkFullPathInfo = new System.Windows.Forms.CheckBox();
            this.lstFiles = new System.Windows.Forms.ListView();
            this.Filename = new System.Windows.Forms.ColumnHeader();
            this.Filepath = new System.Windows.Forms.ColumnHeader();
            this.DateModified = new System.Windows.Forms.ColumnHeader();
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
            this.txtObject = new System.Windows.Forms.TextBox();
            this.txtAddress = new System.Windows.Forms.TextBox();
            this.grpMailInfo = new System.Windows.Forms.GroupBox();
            label2 = new System.Windows.Forms.Label();
            label1 = new System.Windows.Forms.Label();
            this.grpActions.SuspendLayout();
            this.grpFileLocation.SuspendLayout();
            this.grpFiles.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
            this.grpMailInfo.SuspendLayout();
            this.SuspendLayout();
            // 
            // label2
            // 
            resources.ApplyResources(label2, "label2");
            label2.Name = "label2";
            // 
            // label1
            // 
            resources.ApplyResources(label1, "label1");
            label1.Name = "label1";
            // 
            // btnDone
            // 
            resources.ApplyResources(this.btnDone, "btnDone");
            this.btnDone.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnDone.Name = "btnDone";
            this.btnDone.Click += new System.EventHandler(this.btnDone_Click);
            // 
            // btnCreateZipFile
            // 
            resources.ApplyResources(this.btnCreateZipFile, "btnCreateZipFile");
            this.btnCreateZipFile.Name = "btnCreateZipFile";
            this.btnCreateZipFile.Click += new System.EventHandler(this.btnCreateZipFile_Click);
            // 
            // chkOpenFolder
            // 
            resources.ApplyResources(this.chkOpenFolder, "chkOpenFolder");
            this.chkOpenFolder.Name = "chkOpenFolder";
            // 
            // grpActions
            // 
            resources.ApplyResources(this.grpActions, "grpActions");
            this.grpActions.Controls.Add(this.chkOpenFile);
            this.grpActions.Controls.Add(this.btnCreateZipFile);
            this.grpActions.Controls.Add(this.chkOpenFolder);
            this.grpActions.Name = "grpActions";
            this.grpActions.TabStop = false;
            // 
            // chkOpenFile
            // 
            resources.ApplyResources(this.chkOpenFile, "chkOpenFile");
            this.chkOpenFile.Name = "chkOpenFile";
            // 
            // imageList1
            // 
            this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
            resources.ApplyResources(this.imageList1, "imageList1");
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // savZipFileDlg
            // 
            resources.ApplyResources(this.savZipFileDlg, "savZipFileDlg");
            this.savZipFileDlg.OverwritePrompt = false;
            // 
            // grpFileLocation
            // 
            resources.ApplyResources(this.grpFileLocation, "grpFileLocation");
            this.grpFileLocation.Controls.Add(this.btnFilepath);
            this.grpFileLocation.Controls.Add(this.lblFilepath);
            this.grpFileLocation.Controls.Add(this.txtFilepath);
            this.grpFileLocation.Name = "grpFileLocation";
            this.grpFileLocation.TabStop = false;
            // 
            // btnFilepath
            // 
            resources.ApplyResources(this.btnFilepath, "btnFilepath");
            this.btnFilepath.Name = "btnFilepath";
            // 
            // lblFilepath
            // 
            resources.ApplyResources(this.lblFilepath, "lblFilepath");
            this.lblFilepath.Name = "lblFilepath";
            // 
            // txtFilepath
            // 
            resources.ApplyResources(this.txtFilepath, "txtFilepath");
            this.txtFilepath.Name = "txtFilepath";
            // 
            // grpFiles
            // 
            resources.ApplyResources(this.grpFiles, "grpFiles");
            this.grpFiles.Controls.Add(this.chkZipAndMail);
            this.grpFiles.Controls.Add(this.checkBox1);
            this.grpFiles.Controls.Add(this.chkIncludeMisc);
            this.grpFiles.Controls.Add(this.chkSaveSettings);
            this.grpFiles.Controls.Add(this.btnResetList);
            this.grpFiles.Controls.Add(this.btnRemoveSelection);
            this.grpFiles.Controls.Add(this.chkModifiedFiles);
            this.grpFiles.Controls.Add(this.chkFullPathInfo);
            this.grpFiles.Controls.Add(this.lstFiles);
            this.grpFiles.Name = "grpFiles";
            this.grpFiles.TabStop = false;
            // 
            // chkZipAndMail
            // 
            resources.ApplyResources(this.chkZipAndMail, "chkZipAndMail");
            this.chkZipAndMail.Name = "chkZipAndMail";
            this.chkZipAndMail.CheckedChanged += new System.EventHandler(this.chkZipAndMail_CheckedChanged);
            // 
            // checkBox1
            // 
            resources.ApplyResources(this.checkBox1, "checkBox1");
            this.checkBox1.Name = "checkBox1";
            // 
            // chkIncludeMisc
            // 
            resources.ApplyResources(this.chkIncludeMisc, "chkIncludeMisc");
            this.chkIncludeMisc.Name = "chkIncludeMisc";
            // 
            // chkSaveSettings
            // 
            resources.ApplyResources(this.chkSaveSettings, "chkSaveSettings");
            this.chkSaveSettings.Name = "chkSaveSettings";
            // 
            // btnResetList
            // 
            resources.ApplyResources(this.btnResetList, "btnResetList");
            this.btnResetList.Name = "btnResetList";
            // 
            // btnRemoveSelection
            // 
            resources.ApplyResources(this.btnRemoveSelection, "btnRemoveSelection");
            this.btnRemoveSelection.Name = "btnRemoveSelection";
            // 
            // chkModifiedFiles
            // 
            resources.ApplyResources(this.chkModifiedFiles, "chkModifiedFiles");
            this.chkModifiedFiles.Name = "chkModifiedFiles";
            // 
            // chkFullPathInfo
            // 
            resources.ApplyResources(this.chkFullPathInfo, "chkFullPathInfo");
            this.chkFullPathInfo.Name = "chkFullPathInfo";
            // 
            // lstFiles
            // 
            resources.ApplyResources(this.lstFiles, "lstFiles");
            this.lstFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.Filename,
            this.Filepath,
            this.DateModified});
            this.lstFiles.FullRowSelect = true;
            this.lstFiles.LargeImageList = this.imageList1;
            this.lstFiles.Name = "lstFiles";
            this.lstFiles.SmallImageList = this.imageList1;
            this.lstFiles.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.lstFiles.UseCompatibleStateImageBehavior = false;
            this.lstFiles.View = System.Windows.Forms.View.Details;
            // 
            // Filename
            // 
            resources.ApplyResources(this.Filename, "Filename");
            // 
            // Filepath
            // 
            resources.ApplyResources(this.Filepath, "Filepath");
            // 
            // DateModified
            // 
            resources.ApplyResources(this.DateModified, "DateModified");
            // 
            // errorProvider1
            // 
            this.errorProvider1.ContainerControl = this;
            // 
            // txtObject
            // 
            resources.ApplyResources(this.txtObject, "txtObject");
            this.errorProvider1.SetIconAlignment(this.txtObject, ((System.Windows.Forms.ErrorIconAlignment)(resources.GetObject("txtObject.IconAlignment"))));
            this.txtObject.Name = "txtObject";
            this.txtObject.Validating += new System.ComponentModel.CancelEventHandler(this.txtObject_Validating);
            // 
            // txtAddress
            // 
            resources.ApplyResources(this.txtAddress, "txtAddress");
            this.txtAddress.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            this.txtAddress.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.RecentlyUsedList;
            this.errorProvider1.SetIconAlignment(this.txtAddress, ((System.Windows.Forms.ErrorIconAlignment)(resources.GetObject("txtAddress.IconAlignment"))));
            this.txtAddress.Name = "txtAddress";
            this.txtAddress.Validating += new System.ComponentModel.CancelEventHandler(this.txtAddress_Validating);
            // 
            // grpMailInfo
            // 
            resources.ApplyResources(this.grpMailInfo, "grpMailInfo");
            this.grpMailInfo.Controls.Add(this.txtObject);
            this.grpMailInfo.Controls.Add(label2);
            this.grpMailInfo.Controls.Add(this.txtAddress);
            this.grpMailInfo.Controls.Add(label1);
            this.grpMailInfo.Name = "grpMailInfo";
            this.grpMailInfo.TabStop = false;
            // 
            // ZipForm
            // 
            this.AcceptButton = this.btnCreateZipFile;
            resources.ApplyResources(this, "$this");
            this.CancelButton = this.btnDone;
            this.Controls.Add(this.grpMailInfo);
            this.Controls.Add(this.grpFiles);
            this.Controls.Add(this.grpFileLocation);
            this.Controls.Add(this.btnDone);
            this.Controls.Add(this.grpActions);
            this.Name = "ZipForm";
            this.Load += new System.EventHandler(this.ZipForm_Load);
            this.Closed += new System.EventHandler(this.ZipForm_Closed);
            this.grpActions.ResumeLayout(false);
            this.grpFileLocation.ResumeLayout(false);
            this.grpFileLocation.PerformLayout();
            this.grpFiles.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
            this.grpMailInfo.ResumeLayout(false);
            this.grpMailInfo.PerformLayout();
            this.ResumeLayout(false);

        }
        #endregion
        private void ZipForm_Closed(object sender, System.EventArgs e)
        {
            _data.SaveSettings = this.chkSaveSettings.Checked;
        }
        private void btnDone_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        private void btnCreateZipFile_Click(object sender, System.EventArgs e)
        {
            // Save the settings.
            this.GetFormData();
            string fileName = _data.Value(ZipFormData.DataName.SZPath);
            FileInfo fi;

            if (chkZipAndMail.Checked)
            {
                fileName = Path.Combine(Path.GetTempPath(), Path.GetFileName(fileName));
            }
            else
            {
                fi = new FileInfo(
                   fileName);

                // If file exists, ask to overwrite.
                if (fi.Exists)
                {
                    if (MessageBox.Show(
                        this,
                        string.Format(Properties.ZipStudio.msgFileAlreadyExists, fileName),
                        ZipFormData.AppString("Name"),
                        MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                }
            }
            // Create the archive
            ZipArchive archive = new ZipArchive();
            string exceptionMsg = null;
            try
            {
                archive.CreateArchive(fileName,
                    _data.SolutionFilePath, _includeFilePaths,
                    (this.chkFullPathInfo.Checked == false));
            }
            catch (System.Exception ex)
            {
                exceptionMsg = ex.Message;
            }
            fi = new FileInfo(fileName);
            if (fi.Exists)
            {
                if (chkZipAndMail.Checked)
                {
                    using (Mapi mp = new Mapi())
                    {
                        mp.Attach(fileName);
                        mp.AddRecip(txtAddress.Text, null, false);
                        if (!mp.Send(txtObject.Text, string.Format(Properties.ZipStudio.msgMailBody, Path.GetFileName(fileName))))
                            MessageBox.Show(this,
                                string.Format(Properties.ZipStudio.msgFileSendError, Path.GetFileName(fileName), mp.Error()),
                                ZipFormData.AppString("Name"),
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                        else
                            MessageBox.Show(this,
                                string.Format(Properties.ZipStudio.msgFileSended, Path.GetFileName(fileName)),
                                ZipFormData.AppString("Name"),
                                MessageBoxButtons.OK);
                    }
                }
                else
                {
                    MessageBox.Show(
                        this,
                        string.Format(Properties.ZipStudio.msgFileCreated, fileName),
                        ZipFormData.AppString("Name"));

                    // Open the file in associated application.
                    if (this.chkOpenFile.Checked)
                    {
                        Process.Start(_data.Value(ZipFormData.DataName.SZPath));
                    }
                    // Open the containing folder and select.
                    if (this.chkOpenFolder.Checked)
                    {
                        string cmdLine = "/select,\"" +
                            _data.Value(ZipFormData.DataName.SZPath) + "\"";
                        Process.Start("explorer.exe", cmdLine);
                    }
                }
            }
            else
            {
                exceptionMsg = string.Format(Properties.ZipStudio.msgFileNotCreated, fileName);
            }
            if (exceptionMsg != null)
            {
                MessageBox.Show(this, "File '" +
                    exceptionMsg,
                    ZipFormData.AppString("Name"),
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private void btnFilepath_Click(object sender, System.EventArgs e)
        {
            DirectoryInfo di = Directory.GetParent(
                _data.Value(ZipFormData.DataName.SZPath));
            this.savZipFileDlg.InitialDirectory = di.FullName;
            this.savZipFileDlg.Filter = "Zip files (*.zip)|*.zip";
            this.savZipFileDlg.FilterIndex = 1;
            this.savZipFileDlg.RestoreDirectory = true;
            if (this.savZipFileDlg.ShowDialog() == DialogResult.OK)
            {
                this.txtFilepath.Text = this.savZipFileDlg.FileName;
                _data.Value(ZipFormData.DataName.SZPath, this.txtFilepath.Text);
            }
        }

        private void lstFiles_ColumnClick(object sender,
            System.Windows.Forms.ColumnClickEventArgs e)
        {
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == _columnSorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (_columnSorter.Order == SortOrder.Ascending)
                {
                    _columnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    _columnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                _columnSorter.SortColumn = e.Column;
                _columnSorter.Order = SortOrder.Ascending;
            }
            // Perform the sort with these new sort options.
            this.lstFiles.Sort();
        }
        private void SetToolTips(Control.ControlCollection Controls, ToolTip ToolTip)
        {
            foreach (Control control in Controls)
            {
                ToolTip.SetToolTip(control, ZipFormData.HelpString(control));
                if (control.HasChildren)
                {
                    this.SetToolTips(control.Controls, ToolTip);
                }
            }
        }
        private void SetToolTips(ToolTip ToolTip)
        {
            foreach (Control control in this.Controls)
            {
                ToolTip.SetToolTip(control, ZipFormData.HelpString(control));
                if (control.HasChildren)
                {
                    this.SetToolTips(control.Controls, ToolTip);
                }
            }
        }
        private void ZipForm_Load(object sender, System.EventArgs e)
        {
            ToolTip toolTip = new ToolTip();
            this.SetToolTips(toolTip);
        }
        private void chkModifiedFiles_CheckedChanged(object sender, System.EventArgs e)
        {
            _includeFilePaths = this.SetFileGrid(this.lstFiles, _filePaths,
                this.chkModifiedFiles.Checked, _data.StartupTime,
                this.chkIncludeMisc.Checked, _excludeItems);
            this.UpdateFileDisplayStatus();
        }
        private void chkIncludeMisc_CheckedChanged(object sender, System.EventArgs e)
        {
            if (!dataSetting)
            {
                _includeFilePaths = this.SetFileGrid(this.lstFiles, _filePaths,
                this.chkModifiedFiles.Checked, _data.StartupTime,
                this.chkIncludeMisc.Checked, _excludeItems);
                this.UpdateFileDisplayStatus();
            }
        }
        private void btnRemoveSelection_Click(object sender, System.EventArgs e)
        {
            _includeFilePaths = this.SetFileGrid(this.lstFiles, _filePaths,
                this.chkModifiedFiles.Checked, _data.StartupTime,
                this.chkIncludeMisc.Checked, _excludeItems);
            this.UpdateFileDisplayStatus();
        }
        private void btnResetList_Click(object sender, System.EventArgs e)
        {
            _excludeItems.Clear();
            _includeFilePaths = this.SetFileGrid(this.lstFiles, _filePaths,
                this.chkModifiedFiles.Checked, _data.StartupTime,
                this.chkIncludeMisc.Checked, _excludeItems);
            this.UpdateFileDisplayStatus();
        }

        private void chkZipAndMail_CheckedChanged(object sender, EventArgs e)
        {
            grpMailInfo.Visible = chkZipAndMail.Checked;
            grpFileLocation.Visible = !grpMailInfo.Visible;
            chkOpenFile.Enabled = !chkZipAndMail.Checked;
            chkOpenFolder.Enabled = !chkZipAndMail.Checked;
            btnCreateZipFile.Enabled = !chkZipAndMail.Checked || IsValidMail() && IsValidObject();
        }

        private void txtAddress_Validating(object sender, CancelEventArgs e)
        {
            var valid = IsValidMail() && IsValidObject();
            btnCreateZipFile.Enabled = !chkZipAndMail.Checked || valid;
        }

        private bool IsValidMail()
        {
            var valid = System.Text.RegularExpressions.Regex.IsMatch(txtAddress.Text, @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
            if (valid)
            {
                errorProvider1.SetError(txtAddress, string.Empty);
            }
            else
                errorProvider1.SetError(txtAddress, Properties.ZipStudio.msgInvalidMail);
            return valid;
        }

        private void txtObject_Validating(object sender, CancelEventArgs e)
        {
            var valid = IsValidObject() && IsValidMail();
            btnCreateZipFile.Enabled = !chkZipAndMail.Checked || valid;
        }

        private bool IsValidObject()
        {
            var valid = !string.IsNullOrEmpty(txtObject.Text);
            if (valid)
            {
                errorProvider1.SetError(txtObject, string.Empty);
            }
            else
                errorProvider1.SetError(txtObject, Properties.ZipStudio.msgInvalidObject);
            return valid;
        }
    }
    public class ListViewColumnSorter : IComparer
    {
        private ListView _listView;
        private int _columnToSort;
        public int ColumnToSort
        {
            get { return _columnToSort; }
            set { _columnToSort = value; }
        }
        private SortOrder _orderOfSort;
        public SortOrder SortOrder
        {
            get { return _orderOfSort; }
            set { _orderOfSort = value; }
        }
        private CaseInsensitiveComparer ObjectCompare;
        public ListViewColumnSorter(ListView ListView)
        {
            _listView = ListView;
            _columnToSort = 0;
            _orderOfSort = SortOrder.None;
            ObjectCompare = new CaseInsensitiveComparer();
        }
        public int Compare(object x, object y)
        {
            int compareResult;
            ListViewItem listviewX, listviewY;
            listviewX = (ListViewItem)x;
            listviewY = (ListViewItem)y;
            ColumnHeader header = _listView.Columns[_columnToSort];
            if (header.Text.ToLower().IndexOf("date") >= 0)
            {
                DateTime tx = DateTime.Parse(listviewX.SubItems[_columnToSort].Text);
                DateTime ty = DateTime.Parse(listviewY.SubItems[_columnToSort].Text);
                compareResult = DateTime.Compare(tx, ty);
            }
            else
            {
                compareResult = ObjectCompare.Compare(
                    listviewX.SubItems[_columnToSort].Text,
                    listviewY.SubItems[_columnToSort].Text);
            }
            if (_orderOfSort == SortOrder.Ascending)
            {
                return compareResult;
            }
            else if (_orderOfSort == SortOrder.Descending)
            {
                return (-compareResult);
            }
            else
            {
                return 0;
            }
        }
        public int SortColumn
        {
            set { _columnToSort = value; }
            get { return _columnToSort; }
        }
        public SortOrder Order
        {
            set { _orderOfSort = value; }
            get { return _orderOfSort; }
        }
    }
    public class ZipFormData
    {
        public static string StringFromResource(string Name)
        {
            return Properties.ZipStudio.ResourceManager.GetString(Name);
        }
        public static string HelpString(Control Control)
        {
            return ZipFormData.StringFromResource("hlp" + Control.Name);
        }
        public static string AppString(string Name)
        {
            return ZipFormData.StringFromResource("app" + Name);
        }
        private const int _numData = 6;
        public int Count
        {
            get { return _numData; }
        }
        private string[] _names = new string[]
		{
			"SZOpenFile",
			"SZOpenFolder",
			"SZPath",
            "SZFullPathInfo",
            "SZModifiedFiles",
            "SZIncludeMisc"
		};
        public enum DataName
        {
            SZOpenFile = 0,
            SZOpenFolder = 1,
            SZPath = 2,
            SZFullPathInfo = 3,
            SZModifiedFiles = 4,
            SZIncludeMisc = 5
        };
        private string[] _data = new string[_numData];
        public string Name(DataName Name)
        {
            return _names[(int)Name];
        }
        public string Value(DataName Name)
        {
            return _data[(int)Name];
        }
        public void Value(DataName Name, string Value)
        {
            _data[(int)Name] = Value;
        }
        private DateTime _startup = DateTime.Now;
        public DateTime StartupTime
        {
            get { return _startup; }
            set { _startup = value; }
        }
        private string _solutionFilePath = "";
        public string SolutionFilePath
        {
            get { return _solutionFilePath; }
            set { _solutionFilePath = value; }
        }
        private bool _saveSettings = false;
        public bool SaveSettings
        {
            get { return _saveSettings; }
            set { _saveSettings = value; }
        }
        private Hashtable _files = null;
        public Hashtable Files
        {
            get { return _files; }
        }
        public ZipFormData() { }
        public ZipFormData(string SolutionFilePath, DateTime Startup,
            Hashtable Files)
        {
            for (int i = 0; i < _numData; i++)
            {
                _data[i] = "";
            }
            _solutionFilePath = SolutionFilePath;
            _startup = Startup;
            _files = Files;
        }
    }
}
